package top.lwaiting.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import top.lwaiting.domain.User;

import java.util.List;

/**
 * @author YANG
 * @version V1.0
 * @date 2024/1/30 20:51
 * @see IUserMapper
 **/
public interface IUserMapper {
    /**
     * 查询所有用户信息，同时查询出每个用户关联的订单信息
     * @return {@link List}
     */
    List<User> findAllUserAndOrder();

    /**
     * 查询所有用户信息，同时查询出每个用户关联的角色信息
     * @return {@link List}
     */
    List<User> findAllUserAndRole();

    /** 以下使用注解的方式 */

    /**
     * 添加用户
     * @param user {@link User}
     */
    @Insert(value = "INSERT INTO user(username) VALUES(#{username})")
    public void addUser(User user);

    /**
     * 修改用户信息
     * @param user {@link User}
     */
    @Update(value = "UPDATE user SET username = #{username} where id = #{id}")
    public void updateUserAuto(User user);

    /**
     * 查询所有用户信息
     * @return {@link User}
     */
    @Select(value = "SELECT * FROM user")
    public List<User> selectAll();

    /**
     * 删除用户
     * @param user {@link User}
     */
    @Delete(value = "DELETE FROM user WHERE id = #{id}")
    public void deleteUserAuto(User user);

    /**
     * 根据 id 查询用户信息
     * @param id {@link Integer} uid
     * @return {@link User} 用户信息
     */
    @Select(value = "SELECT * FROM user WHERE id = #{id}")
    public User selectUserById(Integer id);

    /**
     * 注解实现 - 一对多关系映射
     * 查询所有用户信息，同时查询出每个用户关联的订单信息
     * @return {@link List}
     */
    @Select(value = "SELECT * FROM user")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "orderList", column = "id", javaType = List.class,
                    many = @Many(select = "top.lwaiting.mapper.IOrderMapper.findOrderByUid"))
    })
    List<User> findAllUserAndOrderAnnotationOneToMany();

    /**
     * 注解实现 - 多对多关系映射
     *  查询所有用户信息，同时查询出每个用户关联的角色信息
     * @return {@link List}
     */
    @Select("SELECT * FROM user")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "roleList", column = "id", javaType = List.class,
                    many = @Many(select = "top.lwaiting.mapper.IRoleMapper.findRoleByUid"))
    })
    List<User> findAllUserAndRoleAnnotationManyToMany();
}
